<?php

namespace app\support;

use app\websocket\WebSocketServer;

/**
 * WebSocket辅助工具类
 * 提供便捷的WebSocket操作方法
 */
class WebSocketHelper
{
    /**
     * 向所有客户端广播消息
     *
     * @param string $message 消息内容
     * @param string $type 消息类型，默认为'broadcast'
     */
    public static function broadcast(string $message, string $type = 'broadcast'): void
    {
        $data = [
            'type' => $type,
            'message' => $message,
            'timestamp' => date('Y-m-d H:i:s')
        ];
        
        WebSocketServer::sendToAllClient(json_encode($data, JSON_UNESCAPED_UNICODE));
    }
    
    /**
     * 向指定客户端发送消息
     *
     * @param string $clientId 客户端ID
     * @param string $message 消息内容
     * @param string $type 消息类型，默认为'message'
     * @return bool 发送是否成功
     */
    public static function sendToClient(string $clientId, string $message, string $type = 'message'): bool
    {
        $data = [
            'type' => $type,
            'message' => $message,
            'timestamp' => date('Y-m-d H:i:s')
        ];
        
        return WebSocketServer::sendToClient($clientId, json_encode($data, JSON_UNESCAPED_UNICODE));
    }
    
    /**
     * 发送订单状态更新通知
     *
     * @param string $orderId 订单ID
     * @param int $status 订单状态
     * @param string $clientId 客户端ID，为空则广播
     */
    public static function notifyOrderStatus(string $orderId, int $status, string $clientId = ''): void
    {
        $statusText = self::getOrderStatusText($status);
        
        $data = [
            'type' => 'order_status_update',
            'orderId' => $orderId,
            'status' => $status,
            'statusText' => $statusText,
            'timestamp' => date('Y-m-d H:i:s')
        ];
        
        $message = json_encode($data, JSON_UNESCAPED_UNICODE);
        
        if (empty($clientId)) {
            WebSocketServer::sendToAllClient($message);
        } else {
            WebSocketServer::sendToClient($clientId, $message);
        }
    }
    
    /**
     * 发送新订单通知（主要用于管理端）
     *
     * @param array $orderData 订单数据
     */
    public static function notifyNewOrder(array $orderData): void
    {
        $data = [
            'type' => 'new_order',
            'order' => $orderData,
            'timestamp' => date('Y-m-d H:i:s')
        ];
        
        WebSocketServer::sendToAllClient(json_encode($data, JSON_UNESCAPED_UNICODE));
    }
    
    /**
     * 发送系统通知
     *
     * @param string $title 通知标题
     * @param string $content 通知内容
     * @param string $level 通知级别：info, warning, error
     */
    public static function notifySystem(string $title, string $content, string $level = 'info'): void
    {
        $data = [
            'type' => 'system_notification',
            'title' => $title,
            'content' => $content,
            'level' => $level,
            'timestamp' => date('Y-m-d H:i:s')
        ];
        
        WebSocketServer::sendToAllClient(json_encode($data, JSON_UNESCAPED_UNICODE));
    }
    
    /**
     * 获取在线客户端数量
     *
     * @return int
     */
    public static function getOnlineCount(): int
    {
        return WebSocketServer::getConnectionCount();
    }
    
    /**
     * 获取所有在线客户端ID
     *
     * @return array
     */
    public static function getOnlineClientIds(): array
    {
        return WebSocketServer::getAllClientIds();
    }
    
    /**
     * 检查客户端是否在线
     *
     * @param string $clientId
     * @return bool
     */
    public static function isClientOnline(string $clientId): bool
    {
        return WebSocketServer::isClientOnline($clientId);
    }
    
    /**
     * 获取订单状态文本
     *
     * @param int $status
     * @return string
     */
    private static function getOrderStatusText(int $status): string
    {
        $statusMap = [
            1 => '待付款',
            2 => '待接单',
            3 => '已接单',
            4 => '派送中',
            5 => '已完成',
            6 => '已取消'
        ];
        
        return $statusMap[$status] ?? '未知状态';
    }
}